angular.module('demand', [])
    .controller('demandCtrl', ['$scope', 'httpService', '$state', '$rootScope', 'alertmsg', function($scope, httpService, $state, $rootScope, alertmsg) {
        if(user && user.company_id) {
            $scope.service.company_id = user.company_id;
        }

        var current_time = new Date().getTime();
        var selectTime = current_time + 60*60*60;
        var timetype = 'booking_date';
        $scope.changeMountOrder = function(bool) {
          $scope.service.mount_order = bool;
          timetype = 'booking_date';
          if(bool) {
            timetype = 'install_date';
          }
        };

        
        if($scope.service && $scope.service.mount_order) {
            timetype = 'install_date';
        }
        
        var opts = {
            minDate: new Date(),
            enableTime: true,
            dateFormat: 'Y-m-d H:i',
            mode: 'single',
            locale: 'zh',
            defaultDate: selectTime,
            onChange: function(selectedDates, dateStr, instance) {
                selectTime = new Date(selectedDates[0]).getTime();
            }
        };

        // 显示默认时间
        if($scope.service) {
            if(!$scope.service.install_date && !$scope.service.booking_date) {
                updateTime(selectTime);
            } else {
                if($scope.service.install_date) {
                    selectTime = $scope.service.install_date * 1000
                    updateTime(selectTime);
                }
                if($scope.service.booking_date) {
                    selectTime = $scope.service.booking_date * 1000
                    updateTime(selectTime);
                }
            }
        }
            

        function updateTime(time) {
            opts.defaultDate = time;
            $.getScript('./lib/flatpickr/dist/l10n/zh.js', function() {
                flatpickr("#demandflatpickr1", opts);
            });
        }
        // updateTime();
            

        function getFetch() {
            httpService.query('web/fetch_source').then(function(res) {
                if (res.code == 200) {
                    $scope.fetchs = res;
                }
            });
        }
        getFetch();

        $scope.issubmit = false;
        $scope.goInfoNext = function(bool) {
            if (!bool) {
                $scope.service[timetype] = parseInt(selectTime / 1000);
                if(timetype == 'booking_date') { delete $scope.service.install_date; }
                if(timetype == 'install_date') { delete $scope.service.booking_date; }
                if ($scope.service[timetype] > parseInt(current_time / 1000)) {
                  $state.go('reservation.information');
                } else {
                  alertmsg("请选择大于现在的时间！");
                }
            } else {
                $scope.issubmit = true;
            }
        };

        $scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
            if (toState.name == 'reservation.demand') {
                $rootScope.step = 1;
            }
        });

    }]);
